package com.ikangtai.shecare.curve.mpchart;


import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.al.DayUnitDSOutput;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class Menses {

    /**
     * BBTMensesStart 里记录的是 dayUnitDSOutputsList 里所有dayUnitDSOutput为月经期开始的下标位置position,
     * BBTMensesEnd 里记录的是 dayUnitDSOutputsList 里所有dayUnitDSOutput为月经期结束的下标位置position.
     */
    private static List<Integer> BBTMensesStart = new ArrayList<>();
    private static List<Integer> BBTMensesEnd = new ArrayList<>();

    private static boolean inited = false;

    public static void init(){
        initBBTMensesStartEndDayList();
        initBBTLineChartxValue2PeriodMap();
        inited = true;
    }

    private static void initBBTMensesStartEndDayList(){
        BBTMensesStart.clear();
        BBTMensesEnd.clear();

        int dayUnitDSOutputsListLen = App.dayUnitDSOutputsList.size();
        AppLog.i("BBTLineChart dayUnitDSOutputsList, dayUnitDSOutputsListLen = " + dayUnitDSOutputsListLen);

        for(int i = 0; i < dayUnitDSOutputsListLen; i++){
            float mensesXAxisValue = BBTLineUtil.getXAxisValue(App.dayUnitDSOutputsList.get(i).date);

            if(isMensesStart(i) && BBTLineUtil.isValidXAxisValue(mensesXAxisValue) || isFirstXAxisValueMenses(mensesXAxisValue, i))
                BBTMensesStart.add(i);

            if(isMensesEnd(i) && BBTLineUtil.isValidXAxisValue(mensesXAxisValue) || isFinalXAxisValueMenses(mensesXAxisValue, i))
                BBTMensesEnd.add(i);
        }
    }

    private static boolean isMensesStart(int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        if(position == 0 && period.equals(DayUnitDSOutput.PERIOD_YJQ)) return true;
        if(position == 0 && !period.equals(DayUnitDSOutput.PERIOD_YJQ)) return false;

        DayUnitDSOutput previousDayUnitDSOutput = App.dayUnitDSOutputsList.get(position - 1);
        String previousDayPeriod = previousDayUnitDSOutput.getPeriod(App.status);

        if(period.equals(DayUnitDSOutput.PERIOD_YJQ) &&
                !previousDayPeriod.equals(DayUnitDSOutput.PERIOD_YJQ))
            return true;
        else
            return false;
    }

    private static boolean isMensesEnd(int position){

        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);
        //AppLog.i("BBTLineChart isMensesEnd, dayUnitDSOutputsList, position = " + position + ", period = " + period);

        if(position == App.dayUnitDSOutputsList.size() - 1 && period.equals(DayUnitDSOutput.PERIOD_YJQ)) return true;
        if(position == App.dayUnitDSOutputsList.size() - 1 && !period.equals(DayUnitDSOutput.PERIOD_YJQ)) return false;

        DayUnitDSOutput NextDayUnitDSOutput = App.dayUnitDSOutputsList.get(position + 1);
        String nextDayPeriod = NextDayUnitDSOutput.getPeriod(App.status);

        if(period.equals(DayUnitDSOutput.PERIOD_YJQ) &&
                !nextDayPeriod.equals(DayUnitDSOutput.PERIOD_YJQ))
            return true;
        else
            return false;
    }

    /**
     * 刚好曲线X轴的第一天为处于月经期，但是在 dayUnitDSOutputsList 中是月经期的中间位置，非起始位置
     * @param mensesXAxisValue
     * @param position
     * @return
     */
    private static boolean isFirstXAxisValueMenses(float mensesXAxisValue, int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        return mensesXAxisValue == BBTLineChart.MIN_XAXIS_DAY && period.equals(DayUnitDSOutput.PERIOD_YJQ);
    }

    /**
     * 刚好曲线X轴的最后一天为处于月经期，但是在 dayUnitDSOutputsList 中是月经期的中间位置，非末位置
     * @param mensesXAxisValue
     * @param position
     * @return
     */
    private static boolean isFinalXAxisValueMenses(float mensesXAxisValue, int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        return mensesXAxisValue == BBTLineChart.MAX_XAXIS_DAY && period.equals(DayUnitDSOutput.PERIOD_YJQ);
    }

    public static void initBBTLineChartxValue2PeriodMap(){
        int BBTMensesStartLen = BBTMensesStart.size();

        for(int i = 0; i < BBTMensesStartLen; i++){
            int mensesStartPoisition = BBTMensesStart.get(i);
            int mensesEndPoisition = BBTMensesEnd.get(i);

            AppLog.i("BBTLineChart Menses background, i = " + i + ", mensesStartPoisition = " + mensesStartPoisition +", mensesEndPoisition = " + mensesEndPoisition);

            initMensesXValue2PeriodMap(mensesStartPoisition, mensesEndPoisition);
        }
    }

    private static void initMensesXValue2PeriodMap(int mensesStartPoisition, int mensesEndPoisition){
        for(int mensesPoistion = mensesStartPoisition; mensesPoistion <= mensesEndPoisition; mensesPoistion ++){
            DayUnitDSOutput mensesDayUnitDSOutput = App.dayUnitDSOutputsList.get(mensesPoistion);
            float mensesXAxisValue = BBTLineUtil.getXAxisValue(mensesDayUnitDSOutput.date);

            AppLog.i("BBTLineChart mensesXAxisValue = " + DateUtil.getSimpleCircleCalendarTopDate(mensesDayUnitDSOutput.date) + ", x = " + BBTLineUtil.getXAxisValue(mensesDayUnitDSOutput.date));

            BBTLineChart.bbtLineChartxValue2PeriodMap.put(mensesXAxisValue, DayUnitDSOutput.PERIOD_YJQ);
        }
    }

    public static boolean isInMenses(long daySeconds){
//        boolean inMenses = false;
//        int BBTMensesListLen = BBTMensesStart.size();
//
//        for(int i = 0; i < BBTMensesListLen; i++) {
//            int mensesStartPoisition = BBTMensesStart.get(i);
//            DayUnitDSOutput mensesStartDayUnitDSOutput = App.dayUnitDSOutputsList.get(mensesStartPoisition);
//
//            int mensesEndPoisition = BBTMensesEnd.get(i);
//            DayUnitDSOutput mensesEndDayUnitDSOutput = App.dayUnitDSOutputsList.get(mensesEndPoisition);
//
//            if(daySeconds >= mensesStartDayUnitDSOutput.date - 12 * 3600 && daySeconds <= mensesEndDayUnitDSOutput.date + 12 * 3600){
//                inMenses = true;
//                break;
//            }
//        }
//
//        return inMenses;
        float xAxisValue = BBTLineUtil.getXAxisValue(daySeconds);
        return BBTLineChart.bbtLineChartxValue2PeriodMap.get(xAxisValue) == DayUnitDSOutput.PERIOD_YJQ;
    }

    public static int getPeriodNum(float xAxisValue){

        int periodNum = -1;

        int BBTMensesStartLen = BBTMensesStart.size();
        if(BBTMensesStartLen == 0)
            return periodNum + 1;

        float BBTMensesStartXAxisValue = BBTLineUtil.getXAxisValue(App.dayUnitDSOutputsList.get(BBTMensesStart.get(0)).date);
        AppLog.i("BBTMarkView BBTMensesStartXAxisValue = " + BBTMensesStartXAxisValue);

        if (xAxisValue < BBTMensesStartXAxisValue)
            periodNum = -1;
        else
            for(int i = 0; i < BBTMensesStartLen; i++) {

                float iBBTMensesStartXAxisValue = BBTLineUtil.getXAxisValue(App.dayUnitDSOutputsList.get(BBTMensesStart.get(i)).date);
                AppLog.i("BBTMarkView i = " + i + ", iBBTMensesStartXAxisValue = " + iBBTMensesStartXAxisValue);

                if(i == BBTMensesStartLen - 1)
                    periodNum = (int) (xAxisValue - iBBTMensesStartXAxisValue);
                else {
                    float iBBTMensesNextStartXAxisValue = BBTLineUtil.getXAxisValue(App.dayUnitDSOutputsList.get(BBTMensesStart.get(i + 1)).date);

                    if (xAxisValue >= iBBTMensesStartXAxisValue && xAxisValue < iBBTMensesNextStartXAxisValue) {
                        periodNum = (int) (xAxisValue - iBBTMensesStartXAxisValue);
                        break;
                    }
                }
            }

        return periodNum + 1;
    }

    public static List<Integer> getBBTMensesStartList(){
        if(!inited)
            init();

        return BBTMensesStart;
    }

    public static List<Integer> getBBTMensesEndList(){
        if(!inited)
            init();

        return BBTMensesEnd;
    }
}
